package com.toilet.easyvideo.security;

import com.toilet.easyvideo.domain.SysUser;
import com.toilet.easyvideo.service.SysUserService;
import jakarta.annotation.Resource;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class SysUserAuthorizeService implements UserDetailsService {

    @Resource
    private SysUserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if (username == null) {
            throw new UsernameNotFoundException("用户名不能为空");
        }
        SysUser sysUser = userService.lambdaQuery().eq(SysUser::getUserName, username).one();
        if (sysUser == null) {
            throw new UsernameNotFoundException("用户名或密码错误");
        }
        return LoginUser.builder().sysUser(sysUser).build();
    }
}
